// pages/editOrder/editOrder.js
const db = wx.cloud.database();
const app = getApp();
const common = require("../../utils/common")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderid:null,
    orderObj:null,
    imgList:null
  },
  getOrder(){
    db.collection("orderList").doc(this.data.orderid).get().then(res=>{
      this.setData({
        orderObj:res.data,
        imgList:res.data.imgList,
      })
    })
  },
  // 取消修改
  cancel(){
    wx.navigateBack();
  },
  async upLoadImg(path) {
    let imgName = new Date().getTime() + "_" + Math.random();
    let extName = path.slice(path.indexOf("."))
    let res = await wx.cloud.uploadFile({
      cloudPath: "orderImg/" + imgName + extName,
      filePath: path,
    })
    return res.fileID
  },
  async getImgList(tempFile) {
    let promise = await tempFile.map(async item => {
      return await this.upLoadImg(item);
    })
    let imgList = await Promise.all(promise);
    return imgList;
  },
  // 添加图片
  async addImg() {
    wx.chooseImage({
      count: 4-this.data.imgList.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
    }).then(async res => {
      wx.showLoading({
        title: '正在加载',
      })
      // 上传到云存储
        let imgList = await this.getImgList(res.tempFilePaths);
        this.setData({
          imgList: this.data.imgList.concat(imgList)
        })
      wx.hideLoading()
    }).catch(e=>{})
  },
  // 删除图片
  delImg(e) {
    let fileName = e.currentTarget.dataset.filename;
    let index = e.currentTarget.dataset.index;
    wx.cloud.deleteFile({
      fileList: [fileName],
      success: res => {
        this.data.imgList.splice(index, 1)
        this.setData({
          imgList: this.data.imgList
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  editOrder(res){
    let obj = res.detail.value;
    if (!obj.detail) {
      wx.showToast({
        title: '请填写问题描述',
        icon:'none'
      })
    }else{
      let status =this.data.orderObj.status;
      let data={
        phone:obj.phone,
        problem_type:obj.problem_type,
        detail:obj.detail,
        imgList:this.data.imgList,
        createTime:new Date().getTime()
      }
      if (status==5) {
        data.status=0;
        data.statusLabel=app.globalData.statusList[0];
        data.approveResult = null;
        data.recommendation = null;
      }
      // db.collection("orderList").doc(this.data.orderid).update({
      //   data:data
      // }).then(res=>{
      //   if (res.stats.updated>0) {
      //     wx.redirectTo({
      //       url: '/pages/success/success?type=update',
      //     })
      //   }
      // })
      common.updateData("orderList",this.data.orderid,data,"update")
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderid=options.id;
    this.setData({
      orderid:orderid
    })
    this.getOrder();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})